package control.elements.gear;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class Gear {
	
	private static String[] gearslots;
	static {
		gearslots = new String[20];
		gearslots[0] = "Main";
		gearslots[1] = "Body";
		gearslots[2] = "Head";
		gearslots[3] = "Sub";
		gearslots[4] = "Neck";
		gearslots[5] = "R. Ear"; 
		gearslots[6] = "L. Ear";
		gearslots[7] = "Hands";
		gearslots[8] = "R. Ring";
		gearslots[9] = "L. Ring";
		gearslots[10] = "Back";
		gearslots[11] = "Range";
		gearslots[12] = "Legs";
		gearslots[13] = "Feet";
		gearslots[14] = "Waist";
		gearslots[15] = "Ammo";
		
		
	}
	private String my_name;
	private int my_slot;
	private int my_lvl;
	private int perp;
	private int refresh;
	private int smn_skill;
	private String my_condition;
	private static ArrayList<Gear> myGearList;
	
	public Gear(final String the_name,final int the_slot,
			final int the_level, final int the_perp, final int the_refresh,
			final int the_skill, final String the_condition) {
		my_name = the_name;
		my_slot = the_slot;
		my_lvl = the_level;
		perp = the_perp;
		refresh = the_refresh;
		smn_skill = the_skill;
		my_condition = the_condition;
	}
	
	/**
	 * 
	 * @return
	 */
	public int getPerp() {
		return perp;
	}
	
	/**
	 * 
	 * @return
	 */
	public int getRefresh() {
		return refresh;
	}
	
	/**
	 * 
	 * @return
	 */
	public int smn_skill() {
		return smn_skill;
	}
	
	/**
	 * 
	 * @return
	 */
	public String getCondition() {
		return my_condition;
	}
	
	/**
	 * 
	 * @return
	 */
	public int getSlot() {
		return my_slot;
	}
	
	public int getLvl() {
		return my_lvl;
	}
	/**
	 * 
	 * @return
	 */
	public String getName() {
		return my_name;
	}
	
	/**
	 * 
	 * @return
	 */
	public static ArrayList<Gear> gearFactory(String a_file) {
		ArrayList<Gear> gearList = new ArrayList<Gear>();
		final File file = new File(a_file);
		try{
			final Scanner s = new Scanner(file);
			while(s.hasNextLine()) {
				final Gear gear = readFile(s.nextLine());
				gearList.add(gear);
			}
		} catch(FileNotFoundException e) {
			e.printStackTrace();
		} 
		myGearList = gearList;
		return myGearList;
	}

	/**
	 * 
	 * @param nextLine
	 * @return
	 */
	private static Gear readFile(String nextLine) {
		final Scanner s = new Scanner(nextLine);
		s.useDelimiter(", *");
	
		Gear g = null;
		if(s.hasNext()){
		final String gear_name = s.next();
		final int gear_slot = s.nextInt();
		final int gear_lvl = s.nextInt();
		final int gear_perp = s.nextInt();
		final int gear_ref = s.nextInt();
		final int gear_skill = s.nextInt();
		final String gear_con = s.next();
		
		g = 
			new Gear(gear_name, gear_slot, gear_lvl, gear_perp, gear_ref,
					gear_skill, gear_con);
		}
		return g;
	}
	
	/**
	 * 
	 */
	public String toString() {
		StringBuilder sb  = new StringBuilder();
		
		sb.append(String.format(gearslots[my_slot] + " [ %-20s", my_name + " ]"));
		sb.append(String.format(" - Level : %-2d", my_lvl));
		String finalS = sb.toString();
		return finalS;
	}
}
